{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setup JSBSim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Make sure that JSBSim Python module is built and installed.\n",
    "- We are in `<jsbsim-root>/doc/basic`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/mnt/c/Users/agodemar/JSBSim_PROJECT/jsbsim-code/doc/basic'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setup Python utility functions/classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run ../python/JSBSim_nb_utils.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A very simple instantiation of a `fdm` object"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instantiate a flight dynamics model object, setting the JSBSim root directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "fdm = jsbsim.FGFDMExec(root_dir='../../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FGFDMExec \n",
      "root dir\t:\tb'../..'\n",
      "aircraft path\t:\tb'../../aircraft'\n",
      "engine path\t:\tb'../../engine'\n",
      "systems path\t:\tb'../../systems'\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(fdm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the value of a property accessing `fdm`'s dictionary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0023768988323037404"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fdm['atmosphere/rho-slugs_ft3']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load an aircraft model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#fdm.load_model('c172x')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check available scripts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['scripts/737_cruise.xml', 'scripts/737_cruise_steady_turn.xml', 'scripts/737_cruise_steady_turn_simplex.xml', 'scripts/737_test.xml', 'scripts/ah1s_flight_test.xml', 'scripts/b171.xml', 'scripts/B737_Runway.xml', 'scripts/b737_runway_new.xml', 'scripts/B747_script1.xml', 'scripts/ball.xml', 'scripts/ball_chute.xml', 'scripts/ball_orbit.xml', 'scripts/ball_orbit_g_torque.xml', 'scripts/c1721.xml', 'scripts/c1722.xml', 'scripts/c1723.xml', 'scripts/c1724.xml', 'scripts/c172_cruise_8K.xml', 'scripts/c172_elevation_test.xml', 'scripts/c172_elevator_doublet.xml', 'scripts/c172_runway_at_rest_cg_shift.xml', 'scripts/c3101.xml', 'scripts/c3104.xml', 'scripts/c3105.xml', 'scripts/cannonball.xml', 'scripts/Concorde_rotate_test.xml', 'scripts/Concorde_runway_test.xml', 'scripts/f16_runway_test.xml', 'scripts/f16_test.xml', 'scripts/F4N_runway_test.xml', 'scripts/J2460.xml', 'scripts/J2461.xml', 'scripts/L410.xml', 'scripts/L4102.xml', 'scripts/mk82_script.xml', 'scripts/ov10_runway.xml', 'scripts/p51d_tail_wind.xml', 'scripts/p51_start_test.xml', 'scripts/paraglider_rotate_test.xml', 'scripts/Short_S23_1.xml', 'scripts/Short_S23_2.xml', 'scripts/Short_S23_3.xml', 'scripts/Short_S23_4.xml', 'scripts/sim_primer.xml', 'scripts/T37.xml', 'scripts/T38.xml', 'scripts/weather-balloon.xml', 'scripts/x151.xml', 'scripts/x152.xml', 'scripts/x153.xml', 'scripts/ZLT-NT-moored-1.xml']\n"
     ]
    }
   ],
   "source": [
    "currentdir = os.getcwd()\n",
    "scriptsdir_path = '../../scripts'\n",
    "scripts = []\n",
    "for f in os.listdir(scriptsdir_path):\n",
    "    script_path = os.path.join(scriptsdir_path, f)\n",
    "    # Does f contains a JSBSim script ?\n",
    "    if CheckXMLFile(script_path, 'runscript'):\n",
    "        #print(script_path)\n",
    "        scripts.append(script_path[len('../../'):])\n",
    "\n",
    "print(scripts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'scripts/737_cruise_steady_turn.xml'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scripts[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fdm.load_script(scripts[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fdm.run_ic()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fdm.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
